package com.loveyukari.controller;

import com.loveyukari.dao.TourDao;
import com.loveyukari.dao.impl.BaseDao;
import com.loveyukari.model.Auth;
import com.loveyukari.model.RoomGoldRank;
import com.loveyukari.model.TourUser;
import com.loveyukari.service.AuthService;
import com.loveyukari.service.LiwuService;
import com.loveyukari.service.RoomGoldRankService;
import com.loveyukari.support.JsonResult;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller

public class TmpController extends BaseDao{


    @Autowired
    private RoomGoldRankService roomGoldRankService;
    @Autowired
    private AuthService authService;
    @Autowired
    private LiwuService liwuService;
    @Autowired
    private TourDao tourDao;

    /**
     * 用于整理数据
     */

    @RequestMapping("/tmp")
    @ResponseBody
    public JsonResult tmp1(HttpServletResponse response){
        List<RoomGoldRank> list = roomGoldRankService.getRoomGoldRankList();
        StringBuilder sb = new StringBuilder();
        for (RoomGoldRank roomGoldRank:
             list) {
            Auth auth = authService.getCacheAuth(roomGoldRank.getUserId());
            sb.append(roomGoldRank.getRank()).append(". ");
            if (roomGoldRank.getRank() < 10){
                sb.append(" ");
            }
            sb.append(roomGoldRank.getGold());
            int l = roomGoldRank.getGold().toString().length();
            for(int i=l;i<8;i++){
                sb.append(" ");
            }
            sb.append(auth.getName()).append("\r\n");
        }
        return new JsonResult(0,"",sb.toString());
    }

    @RequestMapping("/tmp2")
    public String tmp2(MultipartFile[] file,String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            Date dateStart = sdf.parse(dateStr);
            while(true){
                if (dateStart.getTime() >= sdf.parse("20180101").getTime()){
                    break;
                }
                String p = sdf.format(dateStart);
                FileInputStream input = new FileInputStream("/root/yukari_room_liwu.log." + p);
                liwuService.addHistroy(input,dateStart);
                dateStart = DateUtils.addDays(dateStart,1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "test";
    }

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(DateUtils.addDays(date,-30));
    }

    @RequestMapping("/tmp3")
    public String tmp3(MultipartFile[] file){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<TourUser> list = tourDao.selectAll();
        for (TourUser tourUser :
                list) {
            if (tourUser.getExclude() != 0) {
                continue;
            }
            Auth auth = authService.getCacheAuth(tourUser.getAuthId());
            if (auth == null){
                continue;
            }
            List<JSONObject> tags = authService.getAuthBadge(tourUser.getAuthId());
            for (JSONObject json :
                    tags) {
                String name = json.optString("name");
                if ("196".equals(name)){
                    int lv = json.optInt("lv");
                    if (auth.getFansLv() != lv){
                        authService.updateAuthFansLv(lv,tourUser.getAuthId());
                    }
                }
                if ("总监".equals(name)){
                    tourDao.updateTourExclude(tourUser.getId(),1);
                }
            }
            List<JSONObject> yubaList = authService.getAuthYubaFollow(auth.getId());
            for (JSONObject json:
                    yubaList) {
                int groupId = json.optInt("groupId");
                if (groupId == 588){
                    tourDao.updateTourExclude(tourUser.getId(),1);
                }
            }
        }
        return "test";
    }



}
